package vip.xiaonuo.smzq.modular.psba.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import vip.xiaonuo.smzq.core.utils.TransFormUtil;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.KqfwCoordinates;
import vip.xiaonuo.smzq.modular.kcl.service.impl.ZbServiceImpl;
import vip.xiaonuo.smzq.modular.psba.entity.PsbaGtZb;
import vip.xiaonuo.smzq.modular.psba.entity.vo.PsbaGtJbqkVo;
import vip.xiaonuo.smzq.modular.psba.entity.vo.PsbaGtZbVo;
import vip.xiaonuo.smzq.modular.psba.mapper.PsbaGtZbMapper;
import vip.xiaonuo.smzq.modular.psba.service.IPsbaGtZbService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * 评审备案—坐标 服务实现类
 *
 * @author sqq
 * @since 2024-05-22
 */
@Service
public class PsbaGtZbServiceImpl extends ServiceImpl<PsbaGtZbMapper, PsbaGtZb> implements IPsbaGtZbService {

    @Resource
    private PsbaGtZbMapper psbaGtZbMapper;

    @Override
    public Page<PsbaGtZb> page(PsbaGtZb psbaGtZb) {
        QueryWrapper<PsbaGtZb> queryWrapper = new QueryWrapper<>();
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }


    @Override
    public PsbaGtZbVo getPsbaGtZbVo(String psbabh) {
        PsbaGtZbVo psbaGtZbVo = new PsbaGtZbVo();
        try{
            QueryWrapper<PsbaGtZb> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("psbabh",psbabh);
            queryWrapper.orderByDesc("jsmj");
            List<PsbaGtZb> psbaGtZbList = psbaGtZbMapper.selectList(queryWrapper);
            if (psbaGtZbList != null&&psbaGtZbList.size()>0){
                PsbaGtZb psbaGtZb = psbaGtZbList.get(0);
                BeanUtil.copyProperties(psbaGtZb, psbaGtZbVo);
                if (psbaGtZb.getJszb() != null&&psbaGtZb.getJszb().length()>0){
                    //计算坐标
                    psbaGtZbVo.setJszbCoordinatesList(new ZbServiceImpl().getKqfwCoordinates(psbaGtZb.getJszb()));
                    //计算面积
                    psbaGtZbVo.setWktcljsmj(TransFormUtil.CoordinateDisposeWKT3(psbaGtZb.getJszb()));
                }

                if (psbaGtZb.getXmfwzb() != null&&psbaGtZb.getXmfwzb().length()>0){
                    //压覆项目范围坐标
                    psbaGtZbVo.setYfzbCoordinatesList(new ZbServiceImpl().getKqfwCoordinates(psbaGtZb.getXmfwzb()));
                    //压覆面积
                    psbaGtZbVo.setWktyfmj(TransFormUtil.CoordinateDisposeWKT3(psbaGtZb.getXmfwzb()));
                }
                return psbaGtZbVo;
            }
        }catch (Exception e){

        }



        return null;
    }

    /**
     * 添加坐标
     * @param psbaGtZbVo
     */
    @Override
    public void addPsbaGtZb(PsbaGtZbVo psbaGtZbVo) {
        //先执行删除操作，避免数据重复
        QueryWrapper<PsbaGtZb> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("psbabh",psbaGtZbVo.getPsbabh());
        psbaGtZbMapper.delete(queryWrapper);

        PsbaGtZb psbaGtZb=new PsbaGtZb();
        BeanUtil.copyProperties(psbaGtZbVo,psbaGtZb);
        //矿区范围
        if (psbaGtZbVo.getJszbCoordinatesList()!=null){
          String iszb=  ZbServiceImpl.getCoordinatesStr(psbaGtZbVo.getJszbCoordinatesList());
            psbaGtZb.setJszb(iszb);
        }

        if (psbaGtZbVo.getYfzbCoordinatesList()!=null){
            String yfzb=  ZbServiceImpl.getCoordinatesStr(psbaGtZbVo.getYfzbCoordinatesList());
            psbaGtZb.setXmfwzb(yfzb);
        }
        psbaGtZbMapper.insert(psbaGtZb);

    }
}
